package sunger.net.org.dropmenu.tab;

import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

import sunger.net.org.dropmenu.ResourceTable;
import sunger.net.org.dropmenu.tab.listener.CustomTabEntity;
import sunger.net.org.dropmenu.tab.listener.OnTabSelectListener;
import sunger.net.org.dropmenu.tab.utils.TypedAttrUtils;

import java.util.ArrayList;

/**
 * CommonTabList
 */
public class CommonTabList extends DirectionalLayout implements Component.ClickedListener {
    /**
     * title
     */
    private int mTextsize = 30;
    private int mTextSelectColor;
    private int mTextUnselectColor;
    private int mClickPosition = 0;

    /**
     * icon
     */
    private boolean isIconVisible = true;
    private int mIconGravity = LayoutAlignment.LEFT;
    private int mIconWidth = 60;
    private int mIconHeight = 60;
    private int mIconMargin = 5;
    private int mCurrentTab;

    /**
     * listener
     */
    private OnTabSelectListener listener;

    /**
     * entities
     */
    private ArrayList<CustomTabEntity> entities;

    /**
     * 构造器
     *
     * @param context 上下文
     */
    public CommonTabList(Context context) {
        super(context);
        setOrientation(HORIZONTAL);
        setAlignment(LayoutAlignment.CENTER);
    }

    /**
     * 构造器
     *
     * @param context 上下文
     * @param attrSet 属性
     */
    public CommonTabList(Context context, AttrSet attrSet) {
        super(context, attrSet);
        setOrientation(HORIZONTAL);
        setAlignment(LayoutAlignment.CENTER);
        init(attrSet);
    }

    /**
     * 构造器
     *
     * @param context   上下文
     * @param attrSet   属性
     * @param styleName styleName
     */
    public CommonTabList(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        setOrientation(HORIZONTAL);
        setAlignment(LayoutAlignment.CENTER);
        init(attrSet);
    }

    private void init(AttrSet attrSet) {
        mTextsize = TypedAttrUtils.getInteger(attrSet, "tl_Textsize", 45);
        mTextSelectColor = TypedAttrUtils.getIntColor(attrSet, "tl_TextSelectColor", Color.rgb(255, 133, 0));
        mTextUnselectColor = TypedAttrUtils.getIntColor(attrSet, "tl_TextUnselectColor", Color.rgb(79, 79, 79));
        isIconVisible = TypedAttrUtils.getBoolean(attrSet, "tl_IconVisible", true);
        mIconGravity = TypedAttrUtils.getInteger(attrSet, "tl_IconGravity", LayoutAlignment.LEFT);
        mIconWidth = TypedAttrUtils.getInteger(attrSet, "tl_IconWidth", 60);
        mIconHeight = TypedAttrUtils.getInteger(attrSet, "tl_IconHeight", 60);
        mIconMargin = TypedAttrUtils.getInteger(attrSet, "tl_IconMargin", 5);
        mClickPosition = TypedAttrUtils.getInteger(attrSet, "tl_click", 0);
    }

    /**
     * 创建
     */
    public void createTab() {
        removeAllComponents();
        Component component = null;
        for (int index = 0; index < entities.size(); index++) {
            if (mIconGravity == LayoutAlignment.LEFT) {
                component = LayoutScatter.getInstance(getContext())
                        .parse(ResourceTable.Layout_layout_tab_left, null, false);
            } else if (mIconGravity == LayoutAlignment.TOP) {
                component = LayoutScatter.getInstance(getContext())
                        .parse(ResourceTable.Layout_layout_tab_top, null, false);
            } else if (mIconGravity == LayoutAlignment.BOTTOM) {
                component = LayoutScatter.getInstance(getContext())
                        .parse(ResourceTable.Layout_layout_tab_bottom, null, false);
            } else if (mIconGravity == LayoutAlignment.RIGHT) {
                component = LayoutScatter.getInstance(getContext())
                        .parse(ResourceTable.Layout_layout_tab_right, null, false);
            } else {
                component = LayoutScatter.getInstance(getContext())
                        .parse(ResourceTable.Layout_layout_tab_left, null, false);
            }
            assert false;
            Text text = (Text) component.findComponentById(ResourceTable.Id_tv_tab_title);
            text.setTextSize(mTextsize);
            if (index == mClickPosition) {
                text.setTextColor(new Color(mTextSelectColor));
            } else {
                text.setTextColor(new Color(mTextUnselectColor));
            }
            Image image = (Image) component.findComponentById(ResourceTable.Id_iv_tab_icon);
            CustomTabEntity entity = entities.get(index);
            if (isIconVisible) {
                if (mClickPosition == index) {
                    image.setPixelMap(entity.getTabSelectedIcon());
                } else {
                    image.setPixelMap(entity.getTabUnselectedIcon());
                }
                image.setWidth(mIconWidth);
                image.setHeight(mIconHeight);
                image.setMarginRight(mIconMargin);
                image.setScaleMode(Image.ScaleMode.CLIP_CENTER);
            } else {
                image.setVisibility(HIDE);
            }
            text.setText(entity.getTabTitle());
            component.setLayoutConfig(new LayoutConfig(LayoutConfig.MATCH_CONTENT,
                    LayoutConfig.MATCH_PARENT, LayoutAlignment.CENTER, 1));
            addComponent(component);
            component.setClickedListener(this);
        }
    }

    /**
     * 设置数据
     *
     * @param tabEntities mTabEntities
     */
    public void setTabData(ArrayList<CustomTabEntity> tabEntities) {
        this.entities = tabEntities;
        createTab();
    }

    /**
     * 点击
     *
     * @param component 控件
     */
    @Override
    public void onClick(Component component) {
        mClickPosition = getChildIndex(component);
        if (mCurrentTab != mClickPosition) {
            mCurrentTab = mClickPosition;
            if (listener != null) {
                listener.onTabSelect(mClickPosition);
            }
        } else {
            if (listener != null) {
                listener.onTabReselect(mClickPosition);
            }
        }
        for (int index = 0; index < getChildCount(); index++) {
            Component at = getComponentAt(index);
            Image image = (Image) at.findComponentById(ResourceTable.Id_iv_tab_icon);
            Text text = (Text) at.findComponentById(ResourceTable.Id_tv_tab_title);
            if (at == component) {
                image.setPixelMap(entities.get(index).getTabSelectedIcon());
                text.setTextColor(new Color(mTextSelectColor));
            } else {
                image.setPixelMap(entities.get(index).getTabUnselectedIcon());
                text.setTextColor(new Color(mTextUnselectColor));
            }
        }
    }

    /**
     * 设置监听
     *
     * @param listener 监听
     */
    public void setSelectListener(OnTabSelectListener listener) {
        this.listener = listener;
    }
}